package com.yonyou.pmclouds.picture.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.KeyCountVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.mapper.PictureMapper;
import com.yonyou.pmclouds.picture.rmiitf.PicStatistics;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = PicStatistics.class)
public class PicStatisticsImpl implements PicStatistics {
    @Autowired
    private PictureMapper mapper;

    @Override
    public int queryPicCountByProjects(String[] projects, String[] excludeBillTypes, String[] noneStatusBillTypes, Date timeStart, Date timeEnd) throws BusinessException {

        return mapper.countProjectPics(projects, excludeBillTypes, noneStatusBillTypes, timeStart, timeEnd);
    }

    @Override
    public Map<String, Integer> queryPicCountOfPerProjects(String[] projects, String[] excludeBillTypes, String[] noneStatusBillTypes, Date timeStart, Date timeEnd) throws BusinessException {
        KeyCountVO[] keyCountVOS = mapper.countPerProjectPics(projects, excludeBillTypes, noneStatusBillTypes, timeStart, timeEnd);
        if (ArrayUtils.isEmpty(keyCountVOS)) {
            return null;
        }
        Map<String, Integer> keyCountMap = new HashMap<>(keyCountVOS.length);
        for (KeyCountVO vo : keyCountVOS) {
            keyCountMap.put(vo.getRelateKey(), vo.getKeyNum());
        }
        return keyCountMap;
    }

    @Override
    public Map<String, Integer> queryCountOfBillType(String tenantId, String pkProject, String[] excludeBillTypes, String[] noneStatusBillTypes, Date timeStart, Date timeEnd) {
        KeyCountVO[] keyCountVOS = mapper.countPerProjectBillTypePics(pkProject, excludeBillTypes, noneStatusBillTypes, timeStart, timeEnd);
        if (ArrayUtils.isEmpty(keyCountVOS)) {
            return null;
        }
        Map<String, Integer> keyCountMap = new HashMap<>(keyCountVOS.length);
        for (KeyCountVO vo : keyCountVOS) {
            keyCountMap.put(vo.getRelateKey(), vo.getKeyNum());
        }
        return keyCountMap;
    }
}
